package com.naiterui.ehp.bs.search.controller;

import com.naiterui.common.redis.RedisUtil;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.search.exception.ExceptionCodes;
import com.naiterui.ehp.bs.search.service.IThirdDiagnosisService;
import com.naiterui.ehp.bs.search.utils.PropertyValueConstants;
import com.naiterui.ehp.bs.search.vo.ThirdDiagnosisSyncVO;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
 
/**
 * @author guoyongxiang
 * @date 2018/11/19 15:15
 * @since 1.0.0
 */
@Api(tags = SwaggerTag.DOCTOR + SwaggerTag.DIAGNOSIS)
@RequestMapping("search/third/diagnosis")
@RestController
public class SearchThirdDiagnosisController {

    private Logger LOGGER = LoggerFactory.getLogger(SearchThirdDiagnosisController.class);

    @Autowired
    private IThirdDiagnosisService thirdDiagnosisService;

    @RequestMapping("sync")
    public ResponseVO<Void> sync(@RequestBody List<ThirdDiagnosisSyncVO> vo) throws BusinessException {
        LOGGER.info("药品适应关键字同步start size:{}", vo.size());
        long start = System.currentTimeMillis();
        if (vo.isEmpty()) {
            LOGGER.warn("药品适应关键字同步，列表为空");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        Integer effectiveSku = thirdDiagnosisService.sync(vo);
        LOGGER.info("药品适应关键字同步end.cost:{}, size:{}, 有效保存:{}", System.currentTimeMillis() - start, vo.size(), effectiveSku);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    @RequestMapping("sku/indication")
    public ResponseVO<Set<String>> getIndication(Long skuId) {
        Set<String> medicIndicationKeywordSet = RedisUtil.hashOps().hgetObject(PropertyValueConstants.REDIS_MEDIC_DIAGNOSIS_KEY, skuId + "");
        return PublicService.returnResponseVO(medicIndicationKeywordSet);
    }
}
